package com.ayanami.departmentservice.controller;


import com.ayanami.basecommon.exception.BusinessException;
import com.ayanami.departmentservice.entity.TbDepartment;
import com.ayanami.departmentservice.service.TbDepartmentService;
import com.ayanami.utilscommon.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ayanami
 * @since 2020-07-28
 */
@Api("部门操作")
@RestController
@RequestMapping("/departmentservice/tb-department")
@CrossOrigin
public class TbDepartmentController{
    @Autowired
    private TbDepartmentService tbDepartmentService;
    @ApiOperation(value = "查询部门名称并统计部门人数",notes = "查询")
    @GetMapping("/findDeptAndCount")
    public Result findDeptAndCount(){
        return Result.ok().data("departmentList",tbDepartmentService.findDeptAndCount());
    }
    @ApiOperation(value = "分页查询",notes = "返回部门与系主任名称并计数")
    @GetMapping("/findByDeptPage")
    public Result findByDeptPage(@RequestParam(required = false,defaultValue = "1") Integer current,@RequestParam(required = false,defaultValue = "6") Integer pageSize,String name){
        Page<TbDepartment> page=new Page<>(current,pageSize);
        QueryWrapper<TbDepartment> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",name==null?"":name);
        queryWrapper.eq("d.deleted",0);
        IPage<TbDepartment> dept = tbDepartmentService.findDept(page, queryWrapper);
        long total=dept.getTotal();
        return Result.ok().data("total",total).data("departmentData",dept.getRecords());
    }
    @ApiOperation(value = "删除部门",notes = "根据id删除部门")
    @DeleteMapping("/delDept")
    public Result delDept(Integer id){
        boolean b = tbDepartmentService.removeById(id);
        if(b) {
            return Result.ok();
        }else {
            return Result.error().message("错误");
        }
    }
    @ApiOperation(value = "新增部门",notes = "")
    @PostMapping("/addDept")
    public Result addDept(@RequestBody TbDepartment tbDepartment){
        boolean save = tbDepartmentService.save(tbDepartment);
        if(save) {
            return Result.ok();
        }else {
            throw new BusinessException(20001,"没有此用户");
        }
    }
    @ApiOperation(value = "根据id查询部门",notes = "查询")
    @GetMapping("/findDeptById")
    public Result findDeptById(Integer id){
        TbDepartment byId = tbDepartmentService.getById(id);
        return Result.ok().data("departments",byId);
    }
    @ApiOperation(value = "修改部门",notes = "修改")
    @PutMapping("/updDept")
    public Result updDept(@RequestBody TbDepartment tbDepartment){
        boolean updateById = tbDepartmentService.updateById(tbDepartment);
        if(updateById) {
            return Result.ok();
        }else {
            return Result.error().message("错误");
        }
    }
}

